package com.leenmvc.core.dao.iotdb;

import com.leenmvc.core.dao.iotdb.domain.TimeseriesDomain;
import com.leenmvc.core.dao.iotdb.util.IotDbUtil;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.SessionDataSetWrapper;
import org.apache.iotdb.session.pool.SessionPool;
import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
import org.apache.iotdb.tsfile.read.common.Field;
import org.apache.iotdb.tsfile.read.common.RowRecord;
import org.apache.iotdb.tsfile.write.record.Tablet;
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SessionPoolTest {

    static Logger logger = LoggerFactory.getLogger(SessionPoolTest.class);

    private static final String ip = "192.168.3.235";

    public static void main(String[] args) {

        //创建存储组
        createStorageGroup();
        //创建时间序列
//        createTimeSerial();
//        insertRecord();
//        deleteStorage();
//        insertTablet();

    }

    /**
     * 创建存储组
     */
    public static void createStorageGroup() {
        SessionPool pool = new SessionPool(ip, 6667, "root", "root", 3);
        try {
            SessionDataSetWrapper dataSetWrapper = pool.executeQueryStatement("SHOW TIMESERIES root.test.001.temperature");
            while (dataSetWrapper.hasNext()){
                RowRecord next = dataSetWrapper.next();
                List<Field> fields = next.getFields();
                String value = (String) IotDbUtil.getValue(fields.get(3));
                TSDataType tsDataType = TSDataType.valueOf(value);
//                for (Field field : fields) {
//                    Object value = IotDbUtil.getValue(field);
//                    System.out.println(value);
//                }
                long timestamp = next.getTimestamp();
            }
            pool.closeResultSet(dataSetWrapper);
//            pool.setStorageGroup("root.site");
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
        logger.info("创建存储组成功");
        pool.close();
    }


    /**
     * 创建时间序列
     */
    public static void createTimeSerial() {
        SessionPool pool = new SessionPool(ip, 6667, "root", "root", 3);
//        try {
//            boolean b = pool.checkTimeseriesExists("root.site.888.wt03.status2");
//            System.out.println(b);
//        } catch (IoTDBConnectionException e) {
//            e.printStackTrace();
//        } catch (StatementExecutionException e) {
//            e.printStackTrace();
//        }
        try {
//
//            // TSEncoding.GORILLA 数值类型一般这个
//            // TSEncoding.PLAIN  BOOLEAN TXT
            pool.createTimeseries("root.site.888.psName", TSDataType.TEXT, TSEncoding.PLAIN, CompressionType.SNAPPY);
//            pool.createTimeseries("root.site.888.b", TSDataType.TEXT, TSEncoding.PLAIN, CompressionType.SNAPPY);
//            pool.createTimeseries("root.site.888.c", TSDataType.TEXT, TSEncoding.PLAIN, CompressionType.SNAPPY);
//            pool.createTimeseries("root.site.888.d", TSDataType.TEXT, TSEncoding.PLAIN, CompressionType.SNAPPY);
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
        logger.info("创建时间序列");
        pool.close();
    }

    /**
     * 创插入设备数据
     */
    public static void insertRecord() {
        SessionPool pool = new SessionPool(ip, 6667, "root", "root", 3);
        try {
            // TSEncoding.GORILLA 数值类型一般这个
            // TSEncoding.PLAIN  BOOLEAN TXT
            List<String> measurements = new ArrayList<>();
            measurements.add("a");
            measurements.add("b");
            measurements.add("c");
            measurements.add("d");
            List<String> values = new ArrayList<>();
            values.add("1");
            values.add("1");
            values.add("1");
            values.add("1");
            pool.insertRecord("root.site.888", System.currentTimeMillis(), measurements , values);
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
//        logger.info("插入时间序列成功！");
        pool.close();
    }

    /**
     * 删除存储组
     */
    public static void deleteStorage() {
        SessionPool pool = new SessionPool(ip, 6667, "root", "root", 3);
        try {
            pool.deleteStorageGroup("root.Helloworld");
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
        logger.info("删除存储组成功！");
        pool.close();
    }

    /**
     * 批量保存时间序列
     */
    public static Boolean saveTimeseriesBatch(List<TimeseriesDomain> timeseriesDomains, String deviceId){
        SessionPool pool = new SessionPool(ip, 6667, "root", "root", 3);
        timeseriesDomains.forEach(timeseriesDomain->{
            try {
                pool.createTimeseries("root.Helloworld."+timeseriesDomain.getName(), TSDataType.TEXT, TSEncoding.PLAIN, CompressionType.SNAPPY);
            } catch (IoTDBConnectionException e) {
                e.printStackTrace();
            } catch (StatementExecutionException e) {
                e.printStackTrace();
            }
        });
        return  true;
    }

    /**
     *   根据设备Id查询设备信息
     */

    /**
     * 删除测点
     */
    public static void insertTablet() {
        SessionPool pool = new SessionPool(ip, 6667, "root", "root", 3);
        try {
            List<MeasurementSchema> serials = new ArrayList<>();
            for(int i = 0; i < 10; i++) {
                MeasurementSchema measurementSchema = new MeasurementSchema();
                measurementSchema.setMeasurementId("a");
                Map<String, String> props = new HashMap<>();
                props.put("a", "1");
                props.put("b", "2");
                props.put("c", "3");
                props.put("d", "4");
                measurementSchema.setProps(props);
                measurementSchema.setType(TSDataType.TEXT);
                serials.add(measurementSchema);
            }
            Tablet tablet = new Tablet("root.Helloworld", serials);
            pool.insertTablet(tablet);
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
        logger.info("删除存储组成功！");
        pool.close();
    }


}
